package com.ygq.Impl;

import DO.ChaptersDO;
import DO.PPTDO;
import VO.PPTSVO;
import com.ygq.PPTService;
import mapper.ChapterMapper;
import mapper.PPTMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;


@Service
public class PPtServiceImpl implements PPTService {

    @Autowired
    private PPTMapper pptMapper;

    @Autowired
    private ChapterMapper chapterMapper;

    @Override
    public void savePPT(MultipartFile file, Integer chapterId, Integer classId,String path) {
        // 获取文件名
        String fileName = file.getOriginalFilename();
        System.out.println(fileName);

        // 获取后缀名
        int lastIndexOfDot = fileName.lastIndexOf('.');
        String fileExtension = "";

        if (lastIndexOfDot != -1) {
            fileExtension = fileName.substring(lastIndexOfDot + 1);
        }
        // 使用uuid生成文件名保证文件名的唯一性
        String fileNewFile = String.valueOf(UUID.randomUUID()) + '.'+ fileExtension;

        saveFileWithNewName(file, path, fileNewFile);

        String address = path + fileNewFile;

        PPTDO pptdo = new PPTDO();
        pptdo.setAddress(address);
        pptdo.setPptName(fileName);
        pptdo.setChapterId(chapterId);
        pptdo.setClassId(classId);

        // 保存课件
        pptMapper.save(pptdo);
    }

    // 获取课件信息
    public List<List<PPTDO>> getPPTByClassId(Integer classId) {
        List<PPTDO> pptdos = pptMapper.selectByClassId(classId);
        // 一共15章，及一共需要15个小List
        List<PPTDO> c1 = new ArrayList<>();
        List<PPTDO> c2 = new ArrayList<>();
        List<PPTDO> c3 = new ArrayList<>();
        List<PPTDO> c4 = new ArrayList<>();
        List<PPTDO> c5 = new ArrayList<>();
        List<PPTDO> c6 = new ArrayList<>();
        List<PPTDO> c7 = new ArrayList<>();
        List<PPTDO> c8 = new ArrayList<>();
        List<PPTDO> c9 = new ArrayList<>();
        List<PPTDO> c10 = new ArrayList<>();
        List<PPTDO> c11 = new ArrayList<>();
        List<PPTDO> c12 = new ArrayList<>();
        List<PPTDO> c13 = new ArrayList<>();
        List<PPTDO> c14 = new ArrayList<>();
        List<PPTDO> c15 = new ArrayList<>();

        // 根据这个班级id为他分别加入到不同的list中去
        for (PPTDO pptdo : pptdos) {
            Integer chapterId = pptdo.getChapterId();
            switch (chapterId){
                case 1 :
                    c1.add(pptdo);
                    break;
                case 2 :
                    c2.add(pptdo);
                    break;
                case 3 :
                    c3.add(pptdo);
                    break;
                case 4 :
                    c4.add(pptdo);
                    break;
                case 5 :
                    c5.add(pptdo);
                    break;
                case 6 :
                    c6.add(pptdo);
                    break;
                case 7 :
                    c7.add(pptdo);
                    break;
                case 8 :
                    c8.add(pptdo);
                    break;
                case 9 :
                    c9.add(pptdo);
                    break;
                case 10 :
                    c10.add(pptdo);
                    break;
                case 11 :
                    c11.add(pptdo);
                    break;
                case 12 :
                    c12.add(pptdo);
                    break;
                case 13 :
                    c13.add(pptdo);
                    break;
                case 14 :
                    c14.add(pptdo);
                    break;
                case 15 :
                    c15.add(pptdo);
                    break;
            }
        }

        List<List<PPTDO>> listList = new ArrayList<>();
        listList.add(c1);
        listList.add(c2);
        listList.add(c3);
        listList.add(c4);
        listList.add(c5);
        listList.add(c6);
        listList.add(c7);
        listList.add(c8);
        listList.add(c9);
        listList.add(c10);
        listList.add(c11);
        listList.add(c12);
        listList.add(c13);
        listList.add(c14);
        listList.add(c15);
        return listList;
    }

    @Override
    public String getAddress(Integer id) {
        return pptMapper.getAddress(id);
    }


    /**
     *
     * @param id 班级id
     * @return
     */
    public List<PPTSVO> getStudentPPT(Integer id) {

        List<PPTDO> pptdos = pptMapper.selectByClassId(id);

        List<PPTSVO> list = new ArrayList<>();
        for (PPTDO pptdo : pptdos) {
            PPTSVO pptsvo = new PPTSVO();
            pptsvo.setId(pptdo.getId());// 课件id
            pptsvo.setPPTName(pptdo.getPptName());
            Integer chapterId = pptdo.getChapterId();
            ChaptersDO chaptersDO = chapterMapper.getAll(chapterId);
            pptsvo.setChapterNumber(chaptersDO.getChapterNumber());
            pptsvo.setDescription(chaptersDO.getDescription());
            list.add(pptsvo);
        }
        return list;
    }

    // 删除课件
    public Integer deletePPT(Integer id) {
        return pptMapper.deletePPT(id);
    }


    // 文件保存
    public void saveFileWithNewName(MultipartFile file, String filePath, String newFileName) {
        try {
            byte[] bytes = file.getBytes();
            Path path = Paths.get(filePath + File.separator + newFileName);
            Files.write(path, bytes);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
